<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('kernel.profile.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-form-item :label="i18n('kernel.profile.type')" prop="type">
            <el-select
              v-model="dataForm.type"
              :placeholder="i18n('common.please_select') + i18n('kernel.profile.type')" clearable>
              <el-option v-for="(v, k) in profileTypeList" :label="v.text" :key="k" :value="v.value"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item :label="i18n('kernel.profile.name')" prop="name">
            <el-input v-model="dataForm.name" :placeholder="i18n('common.please_input') + i18n('kernel.profile.name')"></el-input>
          </el-form-item>

          <el-form-item :label="i18n('kernel.profile.title')" prop="title">
            <el-input v-model="dataForm.title" :placeholder="i18n('common.please_input') + i18n('kernel.profile.title')"></el-input>
          </el-form-item>

          <el-form-item :label="i18n('kernel.profile.language')" prop="language">
            <el-select
              v-model="dataForm.language"
              :placeholder="i18n('common.please_select') + i18n('kernel.profile.language')" clearable>
              <el-option v-for="(v, k) in languageList" :label="v.text" :key="k" :value="v.value"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item class="mavon" prop="content" :label="i18n('kernel.profile.content')">
            <editor ref="editorRef" :content="dataForm.content"></editor>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="dataFormSubmit(formRef)">
              {{ i18n('common.confirm') }}
            </el-button>
            <el-button @click="resetForm(formRef)">
              {{ i18n('common.reset') }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="profile-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"
  import { useStore } from "@/hooks/store/useStore"
  import Editor from '@/components/form/ckeditor'

  const { user } = useStore()
  const { router, query, i18n, isAuth } = useBase()

  const model = ref('profile')

  const editorRef = ref()
  const profileTypeList = ref([])
  const languageList = ref([])

  const dataForm = reactive({
    id: 0,
    type: '1',
    name: '',
    title: '',
    content: '',
    language: 'zh',
  })

  const dataRule = reactive({
    type: [{
      required: true,
      message: i18n('kernel.profile.rules.type.require'),
      trigger: 'blur',
    }],
    name: [{
      required: true,
      message: i18n('kernel.profile.rules.name.require'),
      trigger: 'blur',
    }],
    title: [{
      required: true,
      message: i18n('kernel.profile.rules.title.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    resetForm
  } = useForm(model.value, dataForm)

  const init = (form) => {
    dataForm.id = query.id || 0

      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/profile/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.type = data.data.type.value
            dataForm.name = data.data.name
            dataForm.title = data.data.title
            dataForm.content = data.data.content
            dataForm.language = data.data.language.value
          }
        })
      }
  }

  // 表单提交
  const dataFormSubmit = (form) => {
    form.validate(async (valid, fields) => {
      if (valid) {
        $http({
          url: $http.adornUrl('/profile/handle'),
          method: 'post',
          data: $http.adornData({
            id: dataForm.id || undefined,
            type: dataForm.type,
            name: dataForm.name,
            title: dataForm.title,
            content: editorRef.value.data,
            language: dataForm.language,
          }),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            $message.success(i18n('common.handle_success'))
            router.go(-1)
          } else {
            $message.error(data.message)
          }
        })
      }
    })
  }

  /**
   * 加载资料类型列表数据
   */
  const loadProfileTypeList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'profile_type',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        profileTypeList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadLanguageList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'language',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        languageList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    init(formRef)

    loadProfileTypeList()
    loadLanguageList()
  })
</script>
